/**
 * 
 */
package com.wuka.controller;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.Date;

import javax.servlet.http.HttpSession;

import org.aspectj.util.FileUtil;
import org.assertj.core.util.DateUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.wuka.model.RestfulLog;
import com.wuka.service.IRestFulLogService;


/**
 * @author zhangliping
 *
 */
@RestController
@RequestMapping("/restful")
public class RestFulController {
	
    @Autowired
    private IRestFulLogService restFulLogService;
	
	
	//实现文件的下载
    //需要说明的是文件的上传和下载不需要其他配置
	@RequestMapping(value="/save", method=RequestMethod.POST)
    public ResponseEntity<byte[]> testResponseEntity(HttpSession session,
			@RequestParam(value = "reqUrl") String reqUrl,
			@RequestParam(value = "reqMethod") String reqMethod,
			@RequestParam(value = "reqParam") String reqParam,
			@RequestParam(value = "resp") String resp,
			@RequestParam(value = "fileName") String fileName) throws IOException{
        
        byte[] body = null;
		
		String path = "c:/restful";
		
		//判断文件名是否有后缀，如果没有后缀就添加默认后缀为txt
		if (fileName.contains(".")) {
			// 包含这个点了代表填写的时候就已经带了文件名
		}else {
			fileName = fileName + ".txt";
		}
		saveAsFile(path + File.separator+ fileName, reqUrl, reqMethod,reqParam, resp);
		
		
		// 请求记录存表
		RestfulLog restfulLog = new RestfulLog();
		restfulLog.setFileName(fileName);
		restfulLog.setReqUrl(reqUrl);
		restfulLog.setReqMethod(reqMethod);
		restfulLog.setReqParam(reqParam);
		restfulLog.setReqResponse(resp);
		restfulLog.setAddUser("system");
		restFulLogService.saveRestFulLong(restfulLog);
		
		
		// 开始处理下载的请求
		InputStream in = new FileInputStream(new File(path + File.separator+ fileName));
        body=new byte[in.available()];
        in.read(body);
        
        HttpHeaders headers=new HttpHeaders();
        //响应头的名字和响应头的值
        headers.add("Content-Disposition", "attachment;filename="+ new String(fileName.getBytes("gbk"),"iso-8859-1"));
        HttpStatus statusCode=HttpStatus.OK;
        
        ResponseEntity<byte[]> response=new ResponseEntity<byte[]>(body, headers, statusCode);
        return response;
    }
		
	
	/**
	 * 保存文件到C盘
	 * @param fileName 文件名
	 * @param reqUrl  请求地址
	 * @param reqMethod 请求方式
	 * @param reqParam 入参
	 * @param resp 响应
	 */
	private void saveAsFile(String fileName, String reqUrl, String reqMethod, String reqParam, String resp) {
		StringBuilder sb = new StringBuilder();
		// 处理请求地址
		if (!StringUtils.isEmpty(reqUrl)) {
			sb.append("请求地址:").append("\r\n").append(reqUrl).append("\r\n").append("\r\n");
		}
		
		// 处理请求方式
		if (!StringUtils.isEmpty(reqMethod)) {
			sb.append("请求方式:").append("\r\n").append(reqMethod).append("\r\n").append("\r\n");
		}
		
		// 处理请求参数
		if (!StringUtils.isEmpty(reqParam)) {
			sb.append("请求参数:").append("\r\n").append(reqParam).append("\r\n").append("\r\n");
		}
		
		// 处理请求结果
		if (!StringUtils.isEmpty(resp)) {
			sb.append("请求结果:").append("\r\n").append(resp).append("\r\n").append("\r\n");
		}
		// 有内容时才生成文件
		if (sb.length() > 0 ) {
			FileUtil.writeAsString(new File(fileName), sb.toString());
		}
	}
	
	@RequestMapping("/getRestFul")
	public RestfulLog getDemo() {
		RestfulLog restfulLog = new RestfulLog();
		restfulLog = restFulLogService.selectByPrimaryKey(1l);
		return restfulLog;
	}
	
}
